using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.BandedGrid;
using System.IO;
using BLL;
using DTO;

namespace WellmeadowsHospital
{
    public partial class frmAddStaff : DevExpress.XtraEditors.XtraUserControl
    {
        StaffBLL staffBLL = new StaffBLL();
        QualificationBLL qualificationBLL = new QualificationBLL();
        ExperienceBLL experienceBLL = new ExperienceBLL();
        public frmAddStaff()
        {
            InitializeComponent();
            BindingData();
        }
        public void BindingData()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = staffBLL.SelectDesc();
            navStaffList.BindingSource = bs;
            gcStaffList1.DataSource = bs;
            //hien thi StaffID cau dong dau tien trong gridview 
            lblStaffID1.Text = gvStaff.GetRowCellValue(gvStaff.SelectedRowsCount - 1, StaffID).ToString();

        }
        public void BindingDataQua()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = qualificationBLL.Select(lblStaffID1.Text);
            navQualification.BindingSource = bs;
            gcQualification1.DataSource = bs;

        }
        public void BindingDataExp()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = experienceBLL.Select(lblStaffID1.Text);
            navExp.BindingSource = bs;
            gcExperience1.DataSource = bs;
        }

        private void btnAddPersional1_Click(object sender, EventArgs e)
        {
            try
            {
                StaffDTO staff = new StaffDTO();
                staff.FirstName = txtFirstName1.Text;
                staff.Lastname = txtLastName1.Text;
                staff.Address = txtAddress1.Text;
                staff.Sex = cbbSex1.SelectedItem.ToString();
                staff.Dob = deDOB1.DateTime;
                staff.Tel = txtTel1.Text;
                staff.Nin = txtNIN1.Text;
                staff.PictureProfile = peImage1.Image;

                staff.Status = "New Staff";


                if (staffBLL.Insert(staff).Equals("Thanh cong"))
                {
                    MessageBox.Show("Insert successful");
                    gcStaffList1.DataSource = staffBLL.SelectDesc();
                    lblStaffID1.Text = gvStaff.GetRowCellValue(gvStaff.SelectedRowsCount - 1, StaffID).ToString();
                }
                else
                {
                    MessageBox.Show(staffBLL.Insert(staff));
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        private void btnEdit2AddSTaff_Click(object sender, EventArgs e)
        {
            try
            {
                int row = gvStaff.SelectedRowsCount;
                StaffDTO staff = new StaffDTO();
                staff.StaffId = lblStaffID1.Text;
                staff.FirstName = txtFirstName1.Text;
                staff.Lastname = txtLastName1.Text;
                staff.Address = txtAddress1.Text;
                staff.Sex = cbbSex1.SelectedItem.ToString();
                staff.Dob = deDOB1.DateTime;
                staff.Tel = txtTel1.Text;
                staff.Nin = txtNIN1.Text;
                staff.PictureProfile = peImage1.Image;


                if (staffBLL.Update(staff).Equals("success"))
                {
                    MessageBox.Show("Edit successful");
                    gcStaffList1.DataSource = staffBLL.SelectDesc();
                    gvStaff.SelectRow(row);

                }
                else
                {
                    MessageBox.Show(staffBLL.Update(staff));
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        private void btnResetAddStaff_Click(object sender, EventArgs e)
        {
            txtFirstName1.Text = "";
            txtLastName1.Text = "";
            txtAddress1.Text = "";
            cbbSex1.SelectedText = "Male";
            deDOB1.EditValue = null;
            txtTel1.Text = "";
            txtNIN1.Text = "";
        }
        public void ResetStaffInfo()
        {
            txtFirstName1.Text = "";
            txtLastName1.Text = "";
            txtAddress1.Text = "";
            cbbSex1.EditValue = "";
            cbbSex1.EditValue = "Male";
            deDOB1.EditValue = "";
            txtTel1.Text = "";
            txtNIN1.Text = "";
            peImage1.EditValue = "";
            btnAddPersional1.Enabled = true;

        }
        private void gridView6_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            lblStaffID1.Text = gvStaff.GetRowCellValue(gvStaff.GetSelectedRows()[0], gcStaffID1).ToString();

            //edit thong tin staff
            ResetStaffInfo();
            if (gvStaff.GetRowCellValue(gvStaff.GetSelectedRows()[0], gcName1).ToString() != null)
            {
                string[] name = gvStaff.GetRowCellValue(gvStaff.GetSelectedRows()[0], gcName1).ToString().Split(' ');
                txtFirstName1.Text = name[0];
                txtLastName1.Text = name[1];
            }
            txtAddress1.Text = gvStaff.GetRowCellValue(gvStaff.GetSelectedRows()[0], gcAddress1).ToString();
            cbbSex1.SelectedItem = gvStaff.GetRowCellValue(gvStaff.GetSelectedRows()[0], gcSex1).ToString();
            deDOB1.DateTime = Convert.ToDateTime(gvStaff.GetRowCellValue(gvStaff.GetSelectedRows()[0], gcDateOfBirth1));
            txtTel1.Text = gvStaff.GetRowCellValue(gvStaff.GetSelectedRows()[0], gcTel1).ToString();
            txtNIN1.Text = gvStaff.GetRowCellValue(gvStaff.GetSelectedRows()[0], gcNIN1).ToString();

            //an hien cac nut add va edit
            btnAddPersional1.Enabled = false;
            //hien thi nut add cau qualification  va  experience 
            btnAddQualification.Enabled = true;
            btnAddExperience1.Enabled = true;
            //reset cac field cua qualification de add moi
            txtType1.Text = "";
            deDate1.EditValue = null;
            txtInstitution1.Text = "";

            //Load anh vao pictureedit

            if (gvStaff.GetRowCellValue(gvStaff.GetSelectedRows()[0], gcImageProfile1) != null)
            {
                // Binary data from MS SQL is received in Base64 format. So you can decode it using the FromBase64String function
                byte[] a = Convert.FromBase64String(gvStaff.GetRowCellValue(gvStaff.GetSelectedRows()[0], gcImageProfile1).ToString().Replace("\"", ""));
                MemoryStream ms = new MemoryStream(a);
                Image image = Image.FromStream(ms);
                peImage1.EditValue = image;
                //can bang hinh anh trong pictureEdit 
                peImage1.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
            }
            else
            {
                peImage1.EditValue = "";
            }
        }


        private void txtSearchAddStaff_KeyPress(object sender, KeyPressEventArgs e)
        {
            gcStaffList1.DataSource = staffBLL.SearchIDName(txtSearchAddStaff.Text);
        }

        private void btnAddQualification_Click(object sender, EventArgs e)
        {
            QualificationDTO qua = new QualificationDTO();
            qua.Staffid = lblStaffID1.Text;
            qua.Type = txtType1.Text;
            qua.Date = deDate1.DateTime;
            qua.Institution = txtInstitution1.Text;
            if (qualificationBLL.Insert(qua).Equals("success"))
            {
                MessageBox.Show("Insert Success");
                BindingDataQua();
            }
            else
            {
                MessageBox.Show(qualificationBLL.Insert(qua));
            }
        }

        private void btnEditAddStaffT_Click(object sender, EventArgs e)
        {
            QualificationDTO qua = new QualificationDTO();
            qua.QualificationID = gvQualification1T.GetRowCellValue(gvQualification1T.GetSelectedRows()[0], gcQualificationID).ToString();
            qua.Type = txtType1.Text;
            qua.Date = deDate1.DateTime;
            qua.Institution = txtInstitution1.Text;
            if (qualificationBLL.Update(qua).Equals("success"))
            {
                MessageBox.Show("Update Successful");
                BindingDataQua();
            }
            else
            {
                MessageBox.Show(qualificationBLL.Update(qua));
            }
        }

        private void btnAddExperience1_Click(object sender, EventArgs e)
        {
            ExperienceDTO exp = new ExperienceDTO();
            exp.StaffID = lblStaffID1.Text;
            exp.Position = txtPosition1.Text;
            exp.StartDate = deStartDate1.DateTime;
            exp.FinishDate = deFinishDate1.DateTime;
            exp.Organization = txtOrganization1.Text;
            if (experienceBLL.Insert(exp).Equals("success"))
            {
                MessageBox.Show("Insert successful");
                BindingDataExp();
            }
            else
            {
                MessageBox.Show(experienceBLL.Insert(exp));
            }
        }

        private void btnEditAddStaff_Click(object sender, EventArgs e)
        {
            ExperienceDTO exp = new ExperienceDTO();
            exp.ExperienceId = gvExperienceAddStaff.GetRowCellValue(gvExperienceAddStaff.GetSelectedRows()[0], gcIDExp).ToString();
            exp.Position = txtPosition1.Text;
            exp.StartDate = deStartDate1.DateTime;
            exp.FinishDate = deFinishDate1.DateTime;
            exp.Organization = txtOrganization1.Text;

            if (experienceBLL.Update(exp).Equals("success"))
            {
                MessageBox.Show("Update successful");
                BindingDataExp();
            }
            else
            {
                MessageBox.Show(experienceBLL.Update(exp));
            }
        }


        private void lblStaffID1_TextChanged(object sender, EventArgs e)
        {
            BindingDataExp();
            BindingDataQua();
            //reset cac field  cua qualification de add moi
            txtType1.Text = "";
            deDate1.EditValue = null;
            txtInstitution1.Text = "";

            //hien thi nut add cau qualification
            btnAddQualification.Enabled = true;

            //reset cac field  cua experience de add moi
            txtPosition1.Text = "";
            deStartDate1.EditValue = null;
            deFinishDate1.EditValue = null;
            txtOrganization1.Text = "";

            //hien nut add cua experience
            btnAddExperience1.Enabled = true;
        }

        private void delete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Are you sure delete this record?", "Message", MessageBoxButtons.YesNoCancel,
        MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                if (staffBLL.DeleteQua(gvQualification1T.GetRowCellValue(gvStaff.GetSelectedRows()[0], gcQualificationID).ToString()))
                {
                    BindingDataQua();
                    MessageBox.Show("Delete successful");
                }
                else
                {
                    MessageBox.Show("Delete fail");
                }
            }
        }


        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Are you sure delete this record?", "Message", MessageBoxButtons.YesNoCancel,
        MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                if (staffBLL.DeleteExp(gvExperienceAddStaff.GetRowCellValue(gvStaff.GetSelectedRows()[0], gcIDExp).ToString()))
                {
                    BindingDataExp();
                    MessageBox.Show("Delete successful");
                }
                else
                {
                    MessageBox.Show("Delete fail");
                }
            }
        }

        private void gvQualification1T_RowClick_1(object sender, RowClickEventArgs e)
        {
            txtType1.Text = gvQualification1T.GetRowCellValue(gvQualification1T.GetSelectedRows()[0], Type).ToString();
            deDate1.DateTime = Convert.ToDateTime(gvQualification1T.GetRowCellValue(gvQualification1T.GetSelectedRows()[0], Date));
            txtInstitution1.Text = gvQualification1T.GetRowCellValue(gvQualification1T.GetSelectedRows()[0], Institution).ToString();

            //an nut add
            btnAddQualification.Enabled = false;
        }

        private void gvExperienceAddStaff_RowClick_1(object sender, RowClickEventArgs e)
        {
            txtPosition1.Text = gvExperienceAddStaff.GetRowCellValue(gvExperienceAddStaff.GetSelectedRows()[0], PositionExp).ToString();
            deStartDate1.DateTime = Convert.ToDateTime(gvExperienceAddStaff.GetRowCellValue(gvExperienceAddStaff.GetSelectedRows()[0], StartDateExp));
            deFinishDate1.DateTime = Convert.ToDateTime(gvExperienceAddStaff.GetRowCellValue(gvExperienceAddStaff.GetSelectedRows()[0], FinishDateExp));
            txtOrganization1.Text = gvExperienceAddStaff.GetRowCellValue(gvExperienceAddStaff.GetSelectedRows()[0], OrganizationExp).ToString();

            //an nut add 
            btnAddExperience1.Enabled = false;
        }
    }

    
}
